$(function() {
	// 初始化日期控件
	$.suredy.datetimepicker.init();

	// 刷新页面
	$.suredy.refreshContent = function(bidId, selector/* css选择器，可以为选择其的数组 */) {
		if (!bidId)
			return;

		var sls = [];

		if ($.isArray(selector)) {
			sls = selector;
		} else if (typeof selector === 'string') {
			sls[sls.length] = selector;
		}

		// 增加操作按钮的刷新
		sls[sls.length] = '#op-btns';

		var activeTabId = $('.suredy-tabs ul.nav-tabs>li.active').attr('id');

		var url = $.suredy.contextPath + '/bid/view/' + bidId;

		$.get(url, function(html, textStatus, jqXHR) {
			var $html = $(html);

			$.each(sls, function(i, val) {
				$(val).html($(val, $html).html());

				$('#' + activeTabId + ' a').tab('show');
			});
		}, 'html');
	};

	var showBidResultForm = function(bidId, status, qt) {
		$.suredy.showModal({
			icon : 'icon-ok',
			title : '投标结果',
			showFoot : false,
			uri : $.suredy.contextPath + '/bid/bid-result/' + bidId + '/' + status + '/' + qt
		});
	};

	// 中标
	$('body').on('click', '.win-bid', function(event) {
		var $this = $(this);
		showBidResultForm($this.data('id'), 'B0003_WIN', $this.data('qt'));
	});

	// 未中标
	$('body').on('click', '.lose-bid', function(event) {
		var $this = $(this);
		showBidResultForm($this.data('id'), 'B0004_LOSE', $this.data('qt'));
	});

	// 流标
	$('body').on('click', '.miss-bid', function(event) {
		var $this = $(this);
		showBidResultForm($this.data('id'), 'B0005_MISS', $this.data('qt'));
	});

	// 流标重投
	$('body').on('click', '.re-bid', function(event) {
		var $this = $(this);
		showBidResultForm($this.data('id'), 'B0002_MISS_REBID', $this.data('qt'));
	});

	// 招标费用支出
	$('body').on('click', '.pay-out', function() {
		$.suredy.showModal({
			icon : 'icon-money',
			title : '费用支出',
			showFoot : false,
			uri : $.suredy.contextPath + '/bid/payout/' + $(this).data('id')
		});
	});

	// 上传投标文件
	$('body').on('click', '.upload-bid-file', function() {
		$.suredy.showModal({
			icon : 'icon-upload',
			title : '投标文件',
			showFoot : false,
			uri : $.suredy.contextPath + '/bid/upload-bid-file/' + $(this).data('id')
		});
	});

	// 移除招标文件
	$('body').on('click', 'a.remove-bid-file', function(event) {
		var $this = $(this);
		var id = $this.data('id');
		var bidId = $this.data('bid-id');

		if (!id)
			alert('无效文件信息，无法删除！');

		$.ajax({
			url : $.suredy.contextPath + '/bid-file/del/' + id,
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '删除文件失败！');
					return false;
				}

				// $this.parents('.file-item').remove();
				$.suredy.refreshContent(bidId, [ '.suredy-tabs' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('删除文件失败！\n\n' + errorThrown);
			}
		});
	});

	// 合同费用支出
	$('body').on('click', '.contract-pay-out', function() {
		$.suredy.showModal({
			icon : 'icon-money',
			title : '合同费用',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/payout/' + $(this).data('id') + '/' + $(this).data('bid-id')
		});
	});

	// 上传合同文件
	$('body').on('click', '.contract-upload-bid-file', function() {
		$.suredy.showModal({
			icon : 'icon-upload',
			title : '合同文件资料',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/upload-contract-file/' + $(this).data('id') + '/' + $(this).data('bid-id')
		});
	});

	// 移除合同文件
	$('body').on('click', 'a.remove-contract-file', function(event) {
		var $this = $(this);
		var id = $this.data('id');

		if (!id)
			alert('无效文件信息，无法删除！');

		$.ajax({
			url : $.suredy.contextPath + '/contract-file/del/' + id,
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '删除文件失败！');
					return false;
				}

				$this.parents('.file-item').remove();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('删除文件失败！\n\n' + errorThrown);
			}
		});
	});

	// 签订合同
	$('body').on('click', '.sign-contract', function() {
		var bidId = $(this).data('bid-id');
		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-plus',
			title : '新建合同',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/create-by-bid?bidId=' + bidId
		});
	});

	// 回收投标保证金
	$('body').on('click', '.getback-tbbzj', function() {
		if (!window.confirm('确认【回收投标保证金】？'))
			return false;

		var $this = $(this);

		$.ajax({
			url : $.suredy.contextPath + '/bid-money/gave-back-money/' + $this.data('id') + '/' + $this.data('moneyid'),
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				$.suredy.refreshContent($this.data('id'), [ '.suredy-tabs' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑表单
	$('body').on('click', '.edit-form', function() {
		var $this = $(this);
		var id = $this.data('id');

		location.href = $.suredy.contextPath + "/bid/edit/" + id;
	});

	// 上传文件（通用）
	$('body').on('click', '.common-file-upload', function() {
		var $this = $(this);
		var bidId = $this.data('bid-id');
		var fileType = $this.data('file-type');
		var suffix = $this.data('suffix');

		$.suredy.showModal({
			icon : 'icon-upload',
			title : '文件上传',
			showFoot : false,
			uri : $.suredy.contextPath + '/bid/common-file-upload/' + bidId + '/' + fileType,
			data : {
				suffix : suffix
			}
		});
	});

	// 删除资金
	$('body').on('click', '.remove-money', function() {
		var $this = $(this);
		var id = $this.data('id');
		var bidId = $this.data('bid-id');

		$.ajax({
			url : $.suredy.contextPath + '/bid-money/delete/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				$.suredy.refreshContent(bidId, [ 'div.bid-payout' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑资金
	$('body').on('click', '.edit-money', function() {
		var $this = $(this);
		var id = $this.data('id');
		var bidId = $this.data('bid-id');

		$.suredy.showModal({
			icon : 'icon-edit',
			title : '修改资金信息',
			showFoot : false,
			uri : $.suredy.contextPath + '/bid-money/edit/' + id + '/' + bidId,
		});
	});

});